<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Post\MomentVerfyOk;
use App\Admin\Actions\Post\MomentVerfyBad;
use App\Admin\Extensions\MomentOp;
use App\Http\Controllers\Controller;
use App\Model\Banner;
use App\Model\Moment;
use App\Model\TaskProgressDay;
use App\Util\AliyunUtil;
use App\Util\ComUtil;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;

class MomentController extends Controller
{
    use ModelForm;

    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('动态管理');
            $content->description('');
            $content->body($this->grid());
        });
    }

    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('动态详情');
            $content->description('');
            $content->body($this->form('edit', $id)->edit($id));
        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('动态创建');
            $content->description('');
            $content->body($this->form());
        });
    }

    protected function grid()
    {
        return Admin::grid(Moment::class, function (Grid $grid) {
            $self = $this;
            if (empty(request()->status)) {
                request()->merge(['status' => Moment::STATUS_VERIFING]);
            }
            $where = [];
            $grid->model()->where($where)->orderBy('is_top', 'desc')->orderBy('id', 'desc');
            $grid->id('动态ID');
            $grid->userProfile()->user_profile_id('用户ID');
            $grid->userProfile()->gender('性别')->display(function ($gender) {
                return $gender == 1 ? '男' : ($gender == 0 ? '女' : '-');
            });
            $grid->userProfile()->nick_name('用户昵称');
            $grid->moment_type('动态类型')->display(function ($data) {
                return Moment::TYPE[$data] ?? '未知';
            });
            $grid->words('动态文字');
            $grid->images('动态图片')->display(function ($data) {
                return ComUtil::buidlImgaeHtml($data);
            });
            $grid->voice('动态声音')->display(function ($data) {
                if (!$data) return '';
                $info = json_decode($data, true);
                $link = AliyunUtil::FILE_HOST . $info['link'];
                return '<audio controls>
                <source src="' . $link . '" type="audio/aac">
                您的浏览器不支持 audio 元素。
              </audio>';
            });

            $grid->video('视频')->display(function ($data) {
                if (!$data) return '';
                $info = json_decode($data, true);
                $link = AliyunUtil::FILE_HOST . $info['link'];
                return '<video width="320" height="240" controls="controls">
  <source src="' . $link . '" type="video/ogg" />
  <source src="' . $link . '" type="video/mp4" />
                您的浏览器不支持 video 标签。
                </video>';
            });

            $grid->created_at('创建时间')->display(function ($time) {
                return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
            });
            $grid->status('状态')->display(function ($status) {
                return Moment::STATUS[$status];
            });
            $grid->is_top('是否置顶')->display(function ($data) {
                return $data == 1 ? '置顶' : '-';
            });

            //禁用导出
            $grid->disableExport();
            //禁用行选择
            // $grid->disableRowSelector();
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('status', '动态状态')->select(Moment::STATUS);
                });
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('userProfile.user_profile_id', '用户ID');
                });
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('moment_type', '类型')->select(Moment::TYPE);
                });
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableView();
                $actions->disableEdit();
                $actions->append(new MomentOp(
                    $actions->getKey(),
                    $actions->row,
                    'moment/verify'
                ));
            });

            $grid->batchActions(function ($batch) {
                $batch->add(new MomentVerfyOk());
                $batch->add(new MomentVerfyBad());
            });
        });
    }

    protected function form($mode = '', $id = '')
    {
        return Admin::form(Banner::class, function (Form $form) use ($mode, $id) {
            $form->text('words', '动态文字');

            $form->tools(function (Form\Tools $tools) {
                // 去掉`列表`按钮
                $tools->disableList();
                // 去掉`删除`按钮
                $tools->disableDelete();
                // 去掉`查看`按钮
                $tools->disableView();
            });
        });
    }

    function verify()
    {
        $id = request('id');
        $status = request('status');
        $action = request('action');
        $record = Moment::find($id);
        if (!$record) return ['ret' => -1, 'message' => '记录不存在'];
        if ($action == 'top') $record->update(['is_top' => $status]);
        else {
            switch ($status) {
                case Moment::STATUS_OK:
                    Moment::veryfyOK($record);
                    break;
                case Moment::STATUS_BAD:
                    Moment::veryfyBad($record);
                    break;
                default:
                    $record->update(['status' => $status]);
                    break;
            }
        }
        if($status == 1){
            $task_total = "task_total + 1";
            TaskProgressDay::addRecord($record->user_id,4,$task_total);
        }
        return ['ret' => 0, 'message' => '操作成功！'];
    }
}
